/**
 * Title: OrderActivity.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2014-1-16 下午2:07:28
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.activities;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.provider.MediaStore;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.logic.common.BaseConfig;
import com.luoxudong.phonenumber.logic.db.model.PhoneNumber;
import com.luoxudong.phonenumber.logic.http.model.response.GetProductDetailRsp;
import com.luoxudong.phonenumber.logic.number.PhoneNumberFactory;
import com.luoxudong.phonenumber.logic.number.interfaces.IPhoneNumbserManager;
import com.luoxudong.phonenumber.service.utils.EvtLog;
import com.luoxudong.phonenumber.ui.common.listener.OnClickAvoidForceListener;
import com.luoxudong.phonenumber.ui.utils.ToastUtil;
import com.luoxudong.phonenumber.ui.widgets.titlebar.ITitleBarOnClickListener;
import com.luoxudong.phonenumber.ui.widgets.titlebar.TitleBarView;

/** 
 * ClassName: OrderActivity
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2014-1-16 下午2:07:28
 */
public class OrderActivity extends BaseActivity{
	public static final int MSG_SHOW_TIP = 1;
	private IPhoneNumbserManager phoneNumbserManager = PhoneNumberFactory.getPhoneNumbserManager();
	private TitleBarView titleBarView = null;
	private PhoneNumber phoneNumber = null;
	private GetProductDetailRsp PhoneNumberDetail = null;
	private ImageView logoBrandsImageView = null;
	private TextView logoBrandsTextView = null;
	private TextView numberTextView = null;
	private TextView orderTotalPriceTextView = null;
	private ImageView idCard1ImageView = null;
	private ImageView idCard2ImageView = null;
	private Button orderBtn = null;
	private String idcard1FilePath = null;
	private String idcard2FilePath = null;
	private Bitmap idcard1Bitmap = null;
	private Bitmap idcard2Bitmap = null;
	
	@Override
	protected int getContentViewLayoutResId() {
		return R.layout.pn_order_layout;
	}

	@Override
	protected void onCreateFindView(Bundle savedInstanceState) {
		titleBarView = (TitleBarView)findViewById(R.id.title_bar);
		logoBrandsImageView = (ImageView)findViewById(R.id.iv_logo_brands);
		logoBrandsTextView = (TextView)findViewById(R.id.tv_brands);
		numberTextView = (TextView)findViewById(R.id.tv_pn_number);
		idCard1ImageView = (ImageView)findViewById(R.id.iv_card_1);
		idCard2ImageView = (ImageView)findViewById(R.id.iv_card_2);
		
		orderTotalPriceTextView = (TextView)findViewById(R.id.tv_order_total_price);
		orderBtn = (Button)findViewById(R.id.btn_order);
		
		titleBarView.setTitle(R.string.pn_detail);
		titleBarView.changeMode(TitleBarView.MODEL_BACK);
		setHideProgressDialog(false);
	}

	@Override
	protected void onCreateAddListener(Bundle savedInstanceState) {
		titleBarView.setTitleBarOnClickListener(titleBarOnClickListener);
		orderBtn.setOnClickListener(onClickListener);
		idCard1ImageView.setOnClickListener(onClickListener);
		idCard2ImageView.setOnClickListener(onClickListener);
	}

	@Override
	protected void onCreateInitData(Bundle savedInstanceState) {
		phoneNumber = (PhoneNumber)getIntent().getSerializableExtra("phoneNumber");
		if (phoneNumber == null)
		{
			finish();
			return;
		}
	}

	@Override
	protected void onCreateTaskLoadData() {
		
	}

	@Override
	protected void onCreateTaskAddView() {
		logoBrandsImageView.setImageResource(R.drawable.brands_yd);
		logoBrandsTextView.setText("中国移动手机");
		numberTextView.setText(getNumberFormat(phoneNumber.getPhoneNumber()));
		orderTotalPriceTextView.setText("200.00");
	}

	@Override
	protected void refreshUI(Object obj) {
		
	}
	
	@Override
	protected void handleMessage(Message msg) {
		switch (msg.what) {
		case MSG_SHOW_TIP:
			ToastUtil.showToast(OrderActivity.this, (String)msg.obj);
			break;

		default:
			break;
		}
	}
	
	private void goback()
	{
		
	}
	
	/**
	 * 自动下拉刷新界面
	 * 
	 */
	public void onRefreshing()
	{
		
	}
	
	private Spanned getNumberFormat(String phoneNumber)
	{
		Spanned htmlString = null;
		if (phoneNumber == null || phoneNumber.length() < 11)
		{
			return Html.fromHtml(phoneNumber);
		}
		
		htmlString = Html.fromHtml("<font color=\"#EF8F65\">" + phoneNumber.substring(0,3) + "</font> <font color=\"#007CC3\">" + phoneNumber.substring(3,7) + "</font> <font color=\"#DA251D\">" + phoneNumber.substring(7,11) + "</font>");
		return htmlString;
	}
	
	private void order()
	{
		
	}
	
	private void pickIdCard1()
	{
	    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 调用系统相机  
	    idcard1FilePath = BaseConfig.CURRENT_USER_HOME_DIR + "1.jpg";
	    Uri imageUri = Uri.fromFile(new File(idcard1FilePath));  
	      
	    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//把照片保存在sd卡中指定位置。  
	      
	    startActivityForResult(intent, 101);  
	}
	
	private void pickIdCard2()
	{
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 调用系统相机  
	    idcard2FilePath = BaseConfig.CURRENT_USER_HOME_DIR + "2.jpg";
	    Uri imageUri = Uri.fromFile(new File(idcard2FilePath));  
	      
	    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//把照片保存在sd卡中指定位置。  
	      
	    startActivityForResult(intent, 102);  
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 101:
			if (idcard1Bitmap != null)
			{
				idcard1Bitmap.recycle();
			}
			
			idcard1Bitmap = getThumbnails(idcard1FilePath, 100);
			idCard1ImageView.setImageBitmap(idcard1Bitmap);
			break;
		case 102:
			if (idcard2Bitmap != null)
			{
				idcard2Bitmap.recycle();
			}
			
			idcard2Bitmap = getThumbnails(idcard2FilePath, 100);
			idCard2ImageView.setImageBitmap(idcard2Bitmap);
			break;
		default:
			break;
		}
	}
	
	
	protected Bitmap getThumbnails(String filePath, int thumbMaxLength)
	{
		Bitmap bitmap = null;
		
		File file = new File(filePath);
		
		if (!file.exists())
		{
			return null;
		}
		
		try {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;// 不会为图片分配内容，只获取图片大小等信息
			BitmapFactory.decodeFile(filePath, options);
			int width = 0;
			int height = 0;
			
			options.inSampleSize = Math.round((float) options.outWidth/ (float) BaseActivity.dip2Px(thumbMaxLength));
			
			if (options.inSampleSize <= 0)
			{
				options.inSampleSize = 1;
			}
			
			EvtLog.e("===", "=======" + options.inSampleSize);
			height = options.outHeight * BaseActivity.dip2Px(thumbMaxLength) / options.outWidth;
			// 重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
			options.outHeight = height;
			options.outWidth = BaseActivity.dip2Px(thumbMaxLength);
			options.inJustDecodeBounds = false;
			

			bitmap = BitmapFactory.decodeFile(filePath, options);
			if (bitmap != null) {
				return bitmap;
			}

		} catch (Exception e) {
			return null;
		}
		
		
		return bitmap;
	}
	
	@Override
	protected void onDestroy() {
		if (idcard1Bitmap != null)
		{
			idcard1Bitmap.recycle();
		}
		
		if (idcard2Bitmap != null)
		{
			idcard2Bitmap.recycle();
		}
		
		super.onDestroy();
	}
	
	private OnClickAvoidForceListener onClickListener = new OnClickAvoidForceListener()
	{

		@Override
		public void onClickAvoidForce(View v) {
			switch (v.getId()) {
			case R.id.btn_order:
				order();
				break;
			case R.id.iv_card_1:
				pickIdCard1();
				break;
			case R.id.iv_card_2:
				pickIdCard2();
				break;
			default:
				break;
			}
		}
		
	};
	
	private ITitleBarOnClickListener titleBarOnClickListener = new ITitleBarOnClickListener() {
		
		@Override
		public void onRightClick(View v) {
		}
		
		@Override
		public void onLeftClick(View v) {
			goback();
			finish();
		}
	};
}
